<?php
    include "../../../config.php";
    //include "../model/tree.php";
    
    Header("content-type: application/x-javascript");
    
    //    Note: Do not change the name of the root node to anything but root without also changing it in the helper functions in this file.
    global $CFG;
    $address = $CFG->wwwroot . 'mod/folio/';
    
    $p = 'folio_tree';
    $url = $CFG->wwwroot;

    $body = <<< END

    function {$p}loadDataForNode(node, fnLoadComplete) {
        var id= node.data.id;
    // -- code to get your data, possibly using Connect --
    
    var sUrl = "{$url}mod/folio/ajax/page_tree_select.php?page_ident=" + node.data.id;
    
//    var tmpNode = new YAHOO.widget.TextNode(id + "label1", node, false);
//    tmpNode = new YAHOO.widget.TextNode(id + "label2", node, false);

    //prepare our callback object
    var callback = {
    
        // Create child nodes.
        success: function(oResponse) {
            var oResults = eval("(" + oResponse.responseText + ")");
            if((oResults.ResultSet.Result) && (oResults.ResultSet.Result.length)) {
                for (var x=0, j=oResults.ResultSet.Result.length; x<j; x++) {
                
                    // Each result will be in ID`Display Title`Linkable Title format.
                    //    Ugly code, but it works.                
                    var s = oResults.ResultSet.Result[x];
                    var column = 1;
                    var i = -1;
                    var linkablecaption = '';
                    var caption = '';
                    var expand = 'false';

                    // For each result, iterate thru using ` as hash marks
                    //        to delimite the id, caption, and linkable caption.
                    while (s.length > 0) {
                        i = s.indexOf('`');
                        if( i < 0 ) {
                            // End.
                            s = "";
                        } else {
                            // Continue building
                            if (column == 4) {
                                // Make sure it hasn't already been added.
                                if (node.tree.getNodeByProperty('id', id) == null) {
                                    linkablecaption = s.substring(0, i);
                                    var myObj = {label: caption, id:id, href: linkablecaption };
                                    var tempNode = new YAHOO.widget.TextNode(myObj, node, false);
                                    tempNode.iconMode  = 1;
                                    if( expand == 'true' ) {
                                        // No children, set loaded to true.
                                        tempNode.dynamicLoadComplete = true;
                                    }  
                                }
                                column = 1;
                            } else if (column == 3) {
                                expand = s.substring(0, i);
                                column = 4;
                            } else if (column == 2) {
                                caption = s.substring(0, i);
                                column = 3;
                            } else {
                                id = s.substring(0, i) ;
                                column = 2;
                            }
                            //alert(column);
                        }
                        s = s.substr(i + 1);
                        //alert( s ) ;
                    }
                }
            }

            //Execute callback method 
            oResponse.argument.fnLoadComplete();
        },
        
        // If fail, continue.
        failure: function(oResponse) {
        },
        
        //our handlers for the XHR response will need the same
        //argument information we got to loadNodeData, so
        //we'll pass those along:
        argument: {
            "node": node,
            "fnLoadComplete": fnLoadComplete
        },
        
        //timeout -- if more than 7 seconds go by, we'll abort
        //the transaction and assume there are no children:
        timeout: 4000
    };
    
    //With our callback object ready, it's now time to 
    //make our XHR call using Connection Manager's
    //asyncRequest method:
    YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
    
    }

END;

    echo $body;
?>